/**
 * Copyright [2020] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.callcenter.storage.redis;

import com.google.common.collect.Lists;
import com.hyts.callcenter.access.CallTaskDao;
import com.hyts.callcenter.model.bo.TaskModel;
import lombok.NonNull;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @project-name:callcenter
 * @package-name:com.hyts.callcenter.engine.dao
 * @author:LiBo/Alex
 * @create-date:2021-12-04 8:20 下午
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: redis服务呼叫任务数据访问层
 */

@ConditionalOnExpression("'redis'.equalsIgnoreCase('${callcenter.storage-model}')")
@Service
public class RedisCallTaskDao implements CallTaskDao {



    @Autowired
    RedissonClient redissonClient;


    /**
     * 加载该bucket下的数据信息控制
     * @param taskId
     * @return
     */
    @Override
    public TaskModel load(@NonNull Long taskId) {
        return (TaskModel) redissonClient.getBucket(String.valueOf(taskId)).get();
    }

    /**
     * 删除相关的数据信息
     * @param taskId
     * @return
     */
    @Override
    public boolean remove(@NonNull Long taskId) {
        return redissonClient.getBucket(String.valueOf(taskId)).delete();
    }

    /**
     * 保存数据信息
     * @param taskModel
     * @return
     */
    @Override
    public boolean save(@NonNull TaskModel taskModel) {
        return redissonClient.getBucket(String.valueOf(taskModel.getTaskId())).trySet(taskModel);
    }

    /**
     * 查询数据信息列表
     * @param taskModel
     * @return
     */
    @Override
    public List<TaskModel> query(@NonNull TaskModel taskModel) {
        TaskModel taskModelList = (TaskModel) redissonClient.getBucket(String.valueOf(taskModel.getTaskId())).get();
        return Lists.newArrayList(taskModelList);
    }

    /**
     * 查询数据信息总数
     * @param taskModel
     * @return
     */
    @Override
    public long count(@NonNull TaskModel taskModel) {
       return redissonClient.getBucket(String.valueOf(taskModel.getTaskId())).isExists()?1:0;
    }

}
